﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DynamicProxy.IInterceptor
{
    /// <summary>
    /// 日志拦截器，为指定方法操作之后添加日志
    /// </summary>
    public class LogInterceptorProxy : Castle.DynamicProxy.IInterceptor
    {
        #region Fields
        private string _message;
        private Action<string> _logger;
        #endregion

        #region Constructors
        public LogInterceptorProxy(string message)
            : this(message, null)
        { }

        public LogInterceptorProxy(string message, Action<string> logger)
        {
            this._message = message;
            _logger = logger;
        }
        #endregion

        #region IInterceptor 成员

        public void Intercept(Castle.DynamicProxy.IInvocation invocation)
        {
            invocation.Proceed();
            string message = string.Format("成功写入日志信息：{0}", this._message);
            if (_logger == null)
                message.AddLogFiles("InterceptorProxy.log");
            else
                _logger(message);
        }

        #endregion
    }
}
